const mongoose = require('mongoose')
const Image = mongoose.model('Image')
const Comment = mongoose.model('Comment')

const statsfn = async () => {
  const imageStats = await Image.aggregate()
    .group({
      _id:null,
        numOfImages: {$sum: 1},
        numOfLikes: {$sum: '$likes'},
        numOfVisits: {$sum: '$visit'}
    })
  const numOfComments = await Comment.countDocuments()
  return {
    ...imageStats[0],
    numOfComments
  }
}

module.exports = statsfn